import asyncio

class AsyncOpenCloseStrategy:
    def __init__(self):
        self.stop_event = asyncio.Event()
        self.tasks = []

    async def _process_product(self):
        try:
            while not self.stop_event.is_set():
                # 业务逻辑...
                await asyncio.sleep(1)
        except asyncio.CancelledError:
            print("任务被取消")

    async def main(self):
        # 启动多个任务
        self.tasks = [asyncio.create_task(self._process_product()) for _ in range(3)]
        await asyncio.gather(*self.tasks)

    def shutdown(self):
        # 设置停止标志并取消所有任务
        self.stop_event.set()
        for task in self.tasks:
            task.cancel()

if __name__ == "__main__":
    strategy = AsyncOpenCloseStrategy()
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(strategy.main())
    finally:
        strategy.shutdown()
        # 清理未完成任务
        pending = asyncio.all_tasks(loop)
        for task in pending:
            task.cancel()
        loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
        loop.close()